package com.chen.controller;

import com.chen.pojo.Court;
import com.chen.pojo.CourtTime;
import com.chen.pojo.Type;
import com.chen.service.court.CourtService;
import com.chen.service.courtTime.CourtTimeService;
import com.chen.service.type.TypeService;
import com.chen.vo.CourtVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/court")
public class CourtFrontController {

    @Resource
    private CourtService courtService;

    @Resource
    private TypeService typeService;

    @Resource
    private CourtTimeService courtTimeService;

    /**
     * 查询球场详情页面
     * @param courtId
     * @param model
     * @return
     */
    @RequestMapping("/{courtId}.html")
    public String detail(@PathVariable Integer courtId, Model model){
        //查询球场详情的方法
        Court court = courtService.findById(courtId);
        //将数据放到模型中
        model.addAttribute("court",court);
        return "detail";
    }

    /**
     * 全部前端球场列表页面
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){

        //查询所有类型的方法
        List<Type> typeList = typeService.queryAllType(null);

        List<CourtTime> timeList = courtTimeService.list();

        //创建条件查询类
        CourtVo courtVo = new CourtVo();
        courtVo.setCourtStatus(0);
        //查询球场列表
        List<Court> courtList = courtService.listTime(courtVo);
        //放到模型中
        model.addAttribute("courtList",courtList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("timeList",timeList);
        model.addAttribute("typeId",0);
        model.addAttribute("timeId",0);
        model.addAttribute("campus","null");

        return "courtList";
    }

    /**
     * 首页跳转类型页面
     * @return
     */
    @RequestMapping("/listHome/{timeId}/{typeId}/{campus}")
    public String listHome(@PathVariable Integer timeId,@PathVariable Integer typeId, @PathVariable String campus,Model model){
        //查询所有类型的方法
        List<Type> typeList = typeService.queryAllType(null);
        List<CourtTime> timeList = courtTimeService.list();
        //创建条件查询类
        CourtVo courtVo = new CourtVo();
        courtVo.setCourtType(typeId);
        courtVo.setCourtStatus(0);
        //查询球场列表
        List<Court> courtList = courtService.listTime(courtVo);
        //放到模型中
        model.addAttribute("courtList",courtList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("timeList",timeList);
        return "courtList";
    }

    /**
     * 前端球场类型列表页面
     * @return
     */
    @RequestMapping("/list/{timeId}/{typeId}")
    public String list(@PathVariable Integer timeId,@PathVariable Integer typeId, Model model){
        //查询所有类型的方法
        List<Type> typeList = typeService.queryAllType(null);
        List<CourtTime> timeList = courtTimeService.list();
        //创建条件查询类
        CourtVo courtVo = new CourtVo();
        courtVo.setCourtType(typeId);
        courtVo.setCourtStatus(0);
        //查询球场列表
        List<Court> courtList = courtService.listTime(courtVo);
        //放到模型中
        model.addAttribute("courtList",courtList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("timeList",timeList);
        model.addAttribute("campus","null");
        return "courtList";
    }

    /**
     * 前端球场列表校区  南区
     * @return
     */
    @RequestMapping("/campusList/{timeId}/{typeId}/{campus}")
    public String campusList(@PathVariable Integer timeId, @PathVariable Integer typeId,@PathVariable String campus,Model model){
        if (typeId == 0 && timeId == 0) {
            //查询所有类型的方法
            List<Type> typeList = typeService.queryAllType(null);
            //查询所有时间列表
            List<CourtTime> timeList = courtTimeService.list();

            //创建条件查询类
            CourtVo courtVo = new CourtVo();
            courtVo.setCourtAddress(campus);
            courtVo.setCourtStatus(0);

            //查询球场列表
            List<Court> courtList = courtService.listTime(courtVo);

            model.addAttribute("courtList", courtList);
            model.addAttribute("typeList", typeList);
            model.addAttribute("timeList", timeList);

        }else {
            if (typeId == 0){
                //查询所有类型的方法
                List<Type> typeList = typeService.queryAllType(null);
                //查询所有时间列表
                List<CourtTime> timeList = courtTimeService.list();
                //创建条件查询类
                CourtVo courtVo = new CourtVo();
                courtVo.setTimeId(timeId);
                courtVo.setCourtAddress(campus);
                courtVo.setCourtStatus(0);
                //查询球场列表
                List<Court> courtList = courtService.listTime(courtVo);
                model.addAttribute("courtList", courtList);
                model.addAttribute("typeList", typeList);
                model.addAttribute("timeList", timeList);
            }
            if (timeId == 0){
                //查询所有类型的方法
                List<Type> typeList = typeService.queryAllType(null);
                //查询所有时间列表
                List<CourtTime> timeList = courtTimeService.list();
                //创建条件查询类
                CourtVo courtVo = new CourtVo();
                courtVo.setCourtType(typeId);
                courtVo.setCourtAddress(campus);
                courtVo.setCourtStatus(0);
                //查询球场列表
                List<Court> courtList = courtService.listTime(courtVo);
                model.addAttribute("courtList", courtList);
                model.addAttribute("typeList", typeList);
                model.addAttribute("timeList", timeList);
            }
            if (timeId != 0 && typeId != 0) {
                //查询所有类型的方法
                List<Type> typeList = typeService.queryAllType(null);
                //查询所有时间列表
                List<CourtTime> timeList = courtTimeService.list();
                //创建条件查询类
                CourtVo courtVo = new CourtVo();
                courtVo.setCourtAddress(campus);
                courtVo.setTimeId(timeId);
                courtVo.setCourtType(typeId);
                courtVo.setCourtStatus(0);
                //查询球场列表
                List<Court> courtList = courtService.listTime(courtVo);
                //放到模型中
                model.addAttribute("courtList", courtList);
                model.addAttribute("typeList", typeList);
                model.addAttribute("timeList", timeList);
            }
        }
        return "courtList";
    }

    /**
     * 前端球场页面  时间段
     * @return
     */
    @RequestMapping("/listTime/{timeId}/{typeId}/{campus}")
    public String listTime(@PathVariable Integer timeId, @PathVariable Integer typeId,@PathVariable String campus, Model model){
        if (timeId != 0 && typeId != 0 && campus .equals("null") ){
            //查询所有类型的方法
            List<Type> typeList = typeService.queryAllType(null);
            //查询所有时间列表
            List<CourtTime> timeList = courtTimeService.list();

            //创建条件查询类
            CourtVo courtVo = new CourtVo();
            courtVo.setCourtNumber(timeId);
            courtVo.setCourtType(typeId);
            courtVo.setCourtStatus(0);
            //查询球场列表
            List<Court> courtList = courtService.listTime(courtVo);
            model.addAttribute("courtList", courtList);
            model.addAttribute("typeList", typeList);
            model.addAttribute("timeList", timeList);
            System.out.println("===========3");
        } else if (typeId != 0 && timeId != 0 && campus != null){
                //查询所有类型的方法
                List<Type> typeList = typeService.queryAllType(null);
                //查询所有时间列表
                List<CourtTime> timeList = courtTimeService.list();

                //创建条件查询类
                CourtVo courtVo = new CourtVo();
                courtVo.setCourtNumber(timeId);
                courtVo.setCourtType(typeId);
                courtVo.setCourtAddress(campus);
                courtVo.setCourtStatus(0);
                //查询球场列表
                List<Court> courtList = courtService.listTime(courtVo);
                model.addAttribute("courtList", courtList);
                model.addAttribute("typeList", typeList);
                model.addAttribute("timeList", timeList);
                System.out.println("===========1");
                System.out.println(campus);
            }else if (timeId != 0 && typeId == 0 && campus.equals("null")){
            //查询所有类型的方法
            List<Type> typeList = typeService.queryAllType(null);
            //查询所有时间列表
            List<CourtTime> timeList = courtTimeService.list();

            //创建条件查询类
            CourtVo courtVo = new CourtVo();
            courtVo.setCourtNumber(timeId);
            courtVo.setCourtStatus(0);
            //查询球场列表
            List<Court> courtList = courtService.listTime(courtVo);
            model.addAttribute("courtList", courtList);
            model.addAttribute("typeList", typeList);
            model.addAttribute("timeList", timeList);
            System.out.println("===========2");
            System.out.println(campus);
        }else if (timeId != 0 && typeId == 0 && campus != "null"){
            //查询所有类型的方法
            List<Type> typeList = typeService.queryAllType(null);
            //查询所有时间列表
            List<CourtTime> timeList = courtTimeService.list();

            //创建条件查询类
            CourtVo courtVo = new CourtVo();
            courtVo.setCourtNumber(timeId);
            courtVo.setCourtAddress(campus);
            courtVo.setCourtStatus(0);
            //查询球场列表
            List<Court> courtList = courtService.listTime(courtVo);
            model.addAttribute("courtList", courtList);
            model.addAttribute("typeList", typeList);
            model.addAttribute("timeList", timeList);
            System.out.println("===========4");
        }
        return "courtList";
    }
}
